<?php
/**
 * @version $Id$
 */
class KitAction extends BaseAction {
	protected $kit,$area_list;
	function _initialize() {
		parent::_initialize();
		self::exist_role(array(14,1));
		$this->kit=new Kit();
		//套件类型：1云购商 2云购店 3代理商 4包内件
		$kit_type = array(1=>'云购商',2=>'云购店',3=>'代理商',4=>'包内件');
		$this->assign('kit_type',$kit_type);
		//开通状态:实体套件：0未开通 1关闭套件 2开通套件｜虚拟包：3未销售 4已销售
		$kit_status = array(0=>'未开通',1=>'关闭套件',2=>'开通套件',3=>'未销售',4=>'已销售');
		$this->assign('kit_status',$kit_status);
		//归属类型：14云采购 13省专营商 12市专营商 10区专营商 22代理商 15云购商 20云购店
		$vesting_type = array(14=>'云采购',13=>'省专营商 ',12=>'市专营商 ',10=>'区专营商',22=>'代理商',15=>'云购商',20=>'云购店');
		$this->assign('vesting_type',$vesting_type);
		//操作类型：1创建 2归属 3修改 4状态 5取消
		$log_type = array(1=>'创建',2=>'归属  ',3=>'修改 ',4=>'状态',5=>'取消');
		$this->assign('log_type',$log_type);
		
		$this->area_list=F('Area');
        if(empty($area)){
            $this->area_list=M('Area')->select();
            $this->area_list=field2key($this->area_list,'id');
            F('Area',$this->area_list);
        }
        $this->assign('Area_list',$this->area_list);
	}
	/**
	 * 区域内所有套件
	 * */
    public function index(){
		$condition = array();
		$_condition = array();
		//主表条件
		$sfield= $_REQUEST['sfield'];
		if ($sfield != 'all' && !empty ($sfield) && !empty ($this->keywords)){
			if($sfield == 'userid'){
				$_condition[$sfield] = $this->keywords;
			}else{
				$condition[$sfield] = $this->keywords;
			}
		}
		//套件类型条件查询
		$type= intval($_REQUEST['type']);
		if ($type){
			$condition['type'] = $type;
		}
		//归属区域条件查询
		$province= intval($_REQUEST['province']);
		if ($province){
			$condition['province'] = $province;
		}
		$city= intval($_REQUEST['city']);
		if ($city){
			$condition['city'] = $city;
		}
		$area= intval($_REQUEST['area']);
		if ($area){
			$condition['area'] = $area;
		}
		//套件状态条件查询
	     $isclose = $_REQUEST [ 'isclose'] ;
	     if ($isclose != 'all' && !empty ($isclose )){
	           if ($isclose == '-1' ){
	                 $condition ['isclose' ] = '0' ;
	          } else {
	                 $condition ['isclose' ] = $isclose;
	          }
        }
		$this -> assign($_REQUEST);
		$order=$this->orderField.' '.$this->orderDirection;
		$totalRows = $this->kit->kp_r_kv_list($condition,$_condition,'all');
		$this->assign ( "totalCount", $totalRows );
		if ($totalRows > 0) {
			$page = new Page ( $totalRows, $this->listrows );
			$pages=$page->show ();
			$list = $this->kit->kp_r_kv_list($condition,$_condition,$page->firstRow . ',' . $page->listRows ,$order);
			$this->assign('list',$list);
		}
		$this->display();
    }
	/**
	 * 我售出的套件
	 * */
	public function sellindex(){
		$where=array();
		$where['userid']=$this->_userid;
		switch ($this->_agent_stype) {
			case 1:
				$where['province']=$this->_province;
				$map['city']=array('GT', 0);
				$map['area']=array('GT', 0);
				$map['isclose']=array('EQ',2);
				$map['_logic'] = 'or';
				$where['_complex'] = $map;
				break;
			case 2:
				$where['city']=$this->_city;
				$map['area']=array('GT', 0);
				$map['isclose']=array('GT',0);
				$map['_logic'] = 'or';
				$where['_complex'] = $map;
				break;
			case 3:
				$where['area']=$this->_area;
				$map['isclose']=array('GT',0);
				$map['_logic'] = 'or';
				$where['_complex'] = $map;
				break;
			case 4:
				$where['isclose']=array('GT',0);
				break;
		}
		$totalRows = $this->kit->kit_list($where,'all');
		if ($totalRows > 0) {
			$page = new Page ( $totalRows, 10 );
			$pages=$page->show2 ();
			$list = $this->kit->kit_list($where,$page->firstRow . ',' . $page->listRows );
			$this->assign('list',$list);
			$this->assign('pages',$pages);
		}
		$this->display('index');
    }
	/**
	 * 我开通的套件
	 * */
	public function openindex(){
		$where=array();
		$where['userid']=$this->_userid;
		$where['kl_type']=4;
		$totalRows = $this->kit->kit_open_list($where,'all');
		if ($totalRows > 0) {
			$page = new Page ( $totalRows, 10 );
			$pages=$page->show2 ();
			$list = $this->kit->kit_open_list($where,$page->firstRow . ',' . $page->listRows );
			$this->assign('list',$list);
			$this->assign('pages',$pages);
		}
		$this->display('index');
    }
	/**
	 * 我可用的套件
	 * */
	public function buyindex(){
		$where=array();
		$where['userid']=$this->_userid;
		switch ($this->_agent_stype) {
			case 1:
				$where['province']=$this->_province;
				$where['city']=array('EQ', 0);
				$where['area']=array('EQ', 0);
				$where['isclose']=array('EQ',0);
				break;
			case 2:
				$where['city']=$this->_city;
				$where['area']=array('EQ', 0);
				$where['isclose']=array('EQ',0);
				break;
			case 3:
				$where['area']=$this->_area;
				$where['isclose']=array('EQ',0);
				break;
			case 4:
				$where['isclose']=array('EQ',0);
				break;
		}
		$totalRows = $this->kit->kit_list($where,'all');
		if ($totalRows > 0) {
			$page = new Page ( $totalRows, 10 );
			$pages=$page->show2 ();
			$list = $this->kit->kit_list($where,$page->firstRow . ',' . $page->listRows );
			$this->assign('list',$list);
			$this->assign('pages',$pages);
		}
		$this->display('index');
    }
	public function edit(){
		$id = intval ( $_REQUEST ['id'] );
		if(empty($id)){
            $this->_ajm(300,'请选择一条数据修改',0);
        }
		if ($_REQUEST ['dosubmit']) {
			$_info = $_POST['info'];
			$_info['starttime'] = strtotime($_POST['info']['starttime']);
			$_info['endtime'] = strtotime($_POST['info']['endtime']);
			$res=$this->kit->kit_edit($_info,array(),array(),$this->userid);
			$arr=object_array(json_decode($res));
			if($arr['status']==1){
				$this->_ajm(200,$arr['msg'],0,U ( 'Kit/index' ) );
			}else{
				$this->_ajm(300,$arr['msg'],0);
			}
		}
		$info = $this->kit->get_kit_detail($id);
		if(empty($info)){
            $this->_ajm(300,'查询数据出错或者查询的数据不存在',0);
        }
		$this->assign('info',$info);
		$this->display();
	}
	public function detail(){
		$id = intval ( $_REQUEST ['id'] );
		if(empty($id)){
            $this->_ajm(300,'请选择一条数据查看',0);
        }
		$info = $this->kit->get_kit_detail($id);
		if(empty($info)){
            $this->_ajm(300,'查询数据出错或者查询的数据不存在',0);
        }
		$vesting = $this->kit->kit_vesting_list(array('kitid'=>$id));
		$log = $this->kit->kit_log_list(array('kitid'=>$id));
		$this->assign('info',$info);
		$this->assign('vesting',$vesting);
		$this->assign('log',$log);
		$this->display();
	}
	public function add(){
		if ($_REQUEST ['dosubmit']) {
			$kitnumber = intval ( $_REQUEST ['kitnumber'] );
			$kittype = intval ( $_REQUEST ['kittype'] );
			if(empty($kitnumber) || $kitnumber < 1){
	            $this->_ajm(300,'数量不可小于1',0);
	        }
			//数量
			$suc_num = 0;
			$suc_i = 0;
			$is_suc = false;
			for($i = 0;$i < $kitnumber;$i++){
		  		$kit_info['cloudsn'] = 0;
				$kit_info['type'] = $kittype;//类型
				$info = $this->kit->kit_add($kit_info,array(),array(),$this->userid,14,$this->userid,'测试套件');//等平台用户登录体系完善后修改用户ID
				$arr=object_array(json_decode($info));
				if($arr['status'] == 1){
					$cloudsn = $this->kit->build_cloudsn(
								array(
								'id' => $arr['id'],
								'cloudsn' => 'LKO******',
			  					'roleid' => 14,
			  					'type' => $kittype,//类型
			  					'userid' => $this->userid),
			  					array('id'=>'套件ID',
			  					'id'=>'套件ID',
			  					'cloudsn'=>'套件编码',
			  					'roleid'=>'归属用户组ID',
			  					'type'=>'套件类型',
			  					'userid'=>'归属用户ID'
								));
					$this->kit->Kit_edit_cloudsn($cloudsn,$arr['id']);
					++$suc_num;
				}
				++$suc_i;
				if($suc_i == $kitnumber){
					$is_suc = true;
				}
		  	}
			if($is_suc == true){
				$msg = '共需生成'.$kitnumber.'套件,成功'.$suc_num.'套件,失败'.($kitnumber-$suc_num).'套件';
				$this->_ajm(200,$msg,0,U ( 'Kit/index' ) );
			}
		}
		$this->display();
	}
	public function vesting(){
		if ($_REQUEST ['dosubmit']) {
			//查询套件是否存在
			$out_kit=array();//存储等待开通或其它操作的套件
			$i_out=0;
			$kitnumber = intval ( $_REQUEST ['kitnumber'] );
			$kittype = intval ( $_REQUEST ['kittype'] );
			$userid = intval ( $_REQUEST ['userid'] );
			$roleid = intval ( $_REQUEST ['roleid'] );
			$province = intval ( $_REQUEST ['province'] );
			$city = intval ( $_REQUEST ['city'] );
			$area = intval ( $_REQUEST ['area'] );
			if(empty($kitnumber) || $kitnumber < 1){
	            $this->_ajm(300,'数量不可小于1',0);
	        }
			$where=array();
			$where['kp.type']=$kittype;
			$where['starttime']=array('EQ', 0);
			$where['endtime']=array('EQ', 0);
			$where['province']=array('EQ', 0);
			$where['city']=array('EQ', 0);
			$where['area']=array('EQ', 0);
			$where['isclose']=array('EQ',0);
			$kit_list = $this->kit->kit_list($where,"0,".$kitnumber);
			foreach ($kit_list as $keys => $values) {
				$out_kit[$i_out++]=$values;
			}
			if(count($out_kit)!=$kitnumber){
				$this->_ajm(300,'可用套件不足，请先生成对应类型套件!',0);
			}
			if(empty($province)&&empty($city)&&empty($area)&&empty($userid)&&empty($roleid)&&$province==0&&$city==0&&$area==0&&$userid==0&&$roleid==0){
				$this->_ajm(300,'帐户不具备接受套件条件!',0);
			}
			//数量
			$suc_num = 0;
			$suc_i = 0;
			$is_suc = false;
			foreach ($out_kit as $key => $value) {
				$res = $this->kit->kit_vesting(array(
				 'id' => $value['id'],
				 'cloudsn' => $value['cloudsn'],
				 'type' => $kittype,//套件类型
				 'province_cn' => $this->area_list[$province]['name'],//套件归属地址
				 'province' => $province,
				 'city_cn' => $this->area_list[$city]['name'],
				 'city' => $city,
				 'area_cn' => $this->area_list[$area]['name'],
				 'area' => $area
				 ),array(),array(),$userid,$roleid,$this->userid,'测试归属');//归属给套件商户,分觥等级,归属人
				 $arr=object_array(json_decode($res));
				if($arr['status']==1){
					++$suc_num;
				}
				++$suc_i;
				if($suc_i == $kitnumber){
					$is_suc = true;
				}
			}
			if($is_suc == true){
				$msg = '共需归属'.$kitnumber.'套件,成功'.$suc_num.'套件,失败'.($kitnumber-$suc_num).'套件';
				$this->_ajm(200,$msg,0,U ( 'Kit/index' ) );
			}
		}
		$this->display();
	}
	/**
	 * 查询客户相关信息返回给客户查看校验用户帐户是否正确
	 * */
	public function ajax_users(){
		$res = array();
		$res['info']='';
		$parent = $_REQUEST['parent'];
		if(!empty($parent)){
			$user=new User();
			$info=$user->get_userinfo_all($parent, 'UserAgent');
			foreach ($info['role_user'] as $key => $value) {
				if(in_array($value['role_id'], array(13,12,10,22))){
					$info['platform_info']['role_id'] = $value['role_id'];
				}
			}
			if($info['platform_info']){//帐户存在
				if($info['platform_info']['isclose'] == 2){
					$res['info'] = $info['platform_info'];
					//F('info_agent',$res['info']);
					$res['status'] = 1;//帐户开通
				}
				if($info['platform_info']['isclose'] == 1){
					$res['status'] = 3;//帐户关闭
				}
				if($info['platform_info']['isclose'] == 0){
					$res['status'] = 2;//帐户未开通
				}
			}else{//帐户不存在
				$res['status'] = 0;//帐户不存在，请先注册云采购帐户
			}
		}	
		else {//系统有误
			$res['status'] = 4;//系统有错误
		}					
		exit(json_encode($res));
	}
}